package com.lhy.service.impl;

import com.lhy.dao.DoctorDao;
import com.lhy.dao.UserDao;
import com.lhy.domain.Doctors;
import com.lhy.domain.Member;
import com.lhy.domain.User;
import com.lhy.service.DoctorService;
import com.lhy.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 汪
 */
@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    DoctorDao doctorDao;
    @Autowired
    UserDao userDao;
    @Override
    public List<Doctors> showAllDoctors() {
        List<Doctors> doctors = doctorDao.showAllDoctors();
        for (Doctors doctor : doctors) {
            String department=doctorDao.getDepartment(doctor.getDepartId());
            doctor.setDepartment(department);
        }
        return doctors;
    }

    @Override
    public int addDoctor(Doctors doctors, User user) {
        user.setCreateDate(new Date());
        userDao.addUserAbdoutDoctor(user);
        List<User> someUser = userDao.findSomeUser(null, user.getRealName(), null);
        Integer id = someUser.get(0).getId();
        doctors.setUserId(id);
        doctorDao.addDoctor(doctors);
        String name = doctors.getName();
        userDao.updateDoctorUser(name,id);
        return 0;
    }

    @Override
    public int deleteDoctor(Integer docId) {
        Integer userId = doctorDao.getDoctorById(docId).getUserId();
        doctorDao.deleteDoctor(docId);
        return userDao.deleteUser(userId);
    }

    @Override
    public int modifyDoctor(Doctors doctors) {
        Integer userId=doctorDao.getDoctorById(doctors.getDocId()).getUserId();
        User user=new User();
        user.setId(userId);
        user.setRealName(doctors.getRealName());
        user.setTel(doctors.getTel());
        user.setUsername(doctors.getName());
        userDao.modifyNameAndTel(user);
        doctorDao.modifyDoctor(doctors);
        return 0;
    }

    @Override
    public List<Doctors> findSomeDoctor(Doctors doctors) {
        List<Doctors> someDoctor = doctorDao.findSomeDoctor(doctors);
        for (Doctors doctor : someDoctor) {
            String department=doctorDao.getDepartment(doctor.getDepartId());
            doctor.setDepartment(department);
        }
        return someDoctor;
    }

    @Override
    public List<Doctors> findSomeDoctor1(Pagination<Doctors> page) {
        List<Doctors> doctors = doctorDao.findSomeDoctor1(page);
        for (Doctors doctor : doctors) {
            String department=doctorDao.getDepartment(doctor.getDepartId());
            doctor.setDepartment(department);
        }
        return doctors;
    }
}
